package com.ljk.study8httpfunction.httputil;

import com.ljk.study8httpfunction.entity.Person;
import feign.*;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;

import java.util.concurrent.TimeUnit;

/**
 * desc: spring cloud 框架官方http请求方法,好处是将http请求接口化
 *
 * @author ljk
 * @date 2021/3/31 20:55
 */
public class FeignUtil {
    public static void main(String... args) {
        //服务器地址名
        String url = "http://127.0.0.1:8080";
        httpSend(url);

    }

    /**
     * 发送请求
     * @param url 服务器地址
     */
    private static void httpSend(String url) {
        HttpFunction service = Feign.builder()
                //自定义序列化 https://github.com/OpenFeign/feign#jackson
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                //自定义语法 https://github.com/OpenFeign/feign#jax-rs
//                .contract(new JAXRSContract())
                //设置超时 https://github.com/OpenFeign/feign#customization
                .options(new Request.Options(1L, TimeUnit.SECONDS, 10L, TimeUnit.SECONDS, true))
                //重试
                .retryer(new Retryer.Default(4000, 5000, 2))
                //注册的接口以及地址
                .target(HttpFunction.class, url);
        //接口调用
        Person contributors = service.contributors2("h1");
        System.out.println(contributors);
    }
}
//@Headers("")
interface HttpFunction{
    /**
     * 使用 @JAXRSContract时采用此语法
     * @param url
     * @return
     */
//    @GET @Path("/{hurl}")
//    Person contributors(@PathParam("hurl")String url);

    /**
     *
     * @param url 实际接口地址
     * @return
     */
    @RequestLine("GET /{hurl}")
    Person contributors2(@Param("hurl")String url);
}